/* Version 0.01 2012.06.27  */

#include "button.h"

Button::Button(float _x, float _y, int _w, int _h, int _marginX, int _marginY, const char* _text,unsigned int _id)
:Displayable(_x, _y, _w, _h),  marginX(_marginX), marginY(_marginY) , bInArea(FALSE), id(_id), btnLayer(NULL), btnText(NULL), bShow(TRUE)
{
	text = _text;
	mtrName = "UI_Button";
	static int in = 0;
	bCheck = TRUE;
	bClick = TRUE;
	INFO buf = mtrName; 
	buf	+= "_Normal";
	btnLayer = addImage(buf, _x, _y, _w, _h);
	btnText = addText(_text,_marginX,_marginY,200,100,0);
	setPos(_x,_y);
	setZOrder(640 - in);
	in++;
}

Button::Button(float _x, float _y, int _w, int _h, int _marginX, int _marginY, const char* _text,unsigned int _id, const char* _mtrName)
:Displayable(_x, _y, _w, _h),  marginX(_marginX), marginY(_marginY) , bInArea(FALSE), id(_id), btnLayer(NULL), btnText(NULL), bShow(TRUE)
{
	text = _text;
	mtrName = _mtrName;
	static int in = 0;
	bCheck = TRUE;
	bClick = TRUE;
	INFO buf = mtrName;
	buf += "_Normal";
	btnLayer=addImage(buf, _x, _y, _w, _h);
	btnText = addText(_text,_marginX,_marginY,200,100,0);
	setPos(_x,_y);
	setZOrder(640 - in);
	in++;
}

void Button::checkArea(int mx, int my)
{
	if(bShow == false)
		return;

	if(isInArea(mx,my))
	{
		if(!bInArea)
		{
			INFO buf = mtrName;
			buf	+= "_Hover";
			btnLayer->setImage(buf);
		}
		bInArea = TRUE;
		
	}
	else
	{
		if(bInArea)
		{
			INFO buf = mtrName;
			buf += "_Normal";
			btnLayer->setImage(buf);
		}
		bInArea = FALSE;
		
	}
}

void Button::setID(int _id)
{
	id = _id;
}

void Button::setMtrName(const char* _mtrName)
{
	mtrName = _mtrName; 
}

void Button::setText(const char* _text)
{
	text = _text;;
}


int Button::beClicked()
{
	return id;
}

void Button::draw(HDC hdc)
{
	
	Displayable::draw(hdc);
	if(bInArea)
		Rectangle(hdc, (int)(x+2), (int)(y+2), (int)(x+w-2), (int)(y+h-2));
	//TextOut(hdc, (int)(x+marginX), (int)(y+marginY), text.c_str(), text.size());
}

void Button::showBtn(bool _bShow)
{
	bShow = _bShow;
	btnLayer->show(bShow);
	if(bShow == false)
		btnText->SetText(" ",200,100,0);
	else
		btnText->SetText(text, 200, 100, 0);
		
}